//
//  ZBLiveMarquee.m
//  ZhiBo
//
//  Created by huangtie on 2017/11/29.
//  Copyright © 2017年 huangtie. All rights reserved.
//

#import "ZBLiveMarquee.h"

@interface ZBLiveMarquee()

@property (nonatomic , strong) UILabel *textLabel;

@end

@implementation ZBLiveMarquee

- (instancetype)initWithSize:(CGSize)size font:(UIFont *)font textColor:(UIColor *)color
{
    self = [super init];
    if (self) {
        self.size = size;
        self.clipsToBounds = YES;
        self.space = 8;
        self.moveCount = 5;
        
        self.textLabel = [[UILabel alloc] init];
        self.textLabel.font = font;
        self.textLabel.textColor = color;
        [self addSubview:self.textLabel];
    }
    return self;
}

- (void)begin:(NSString *)text
{
    self.textLabel.text = text;
    [self.textLabel sizeToFit];
    self.textLabel.centerY = self.height / 2;
    [self move:1];
}

- (void)move:(NSInteger)index
{
    if (index > self.moveCount) {
        [self.delegate marqueeDidRunEnd];
        return;
    }

    self.textLabel.left = index == 1 ? 0 : self.width;
    
    [UIView animateWithDuration:self.space animations:^{
        self.textLabel.right = 0;
    } completion:^(BOOL finished) {
        [self move:index + 1];
    }];
}

@end
